/*
 * @(#) MySqlSQLDialect.java 2011-6-9
 *
 * Copyright (c) 2010, Strong Technology. All Rights Reserved.
 * Strong Technology. CONFIDENTIAL
 */
package cn.strong.gweva.common.integration.dao.impl;

import cn.strong.gweva.common.integration.dao.Dialect;

/**
 * 分页处理用MySqlSQL解释器
 * 
 * @author lilanfei
 * @version 1.0
 * @since 2011-6-9
 */
public class MySqlSQLDialect implements Dialect
{
	//SQL结束符
	protected static final String SQL_END_DELIMITER = ";";


	/**
	 * 
	 * 取得分页处理的SQL
	 * @param sql SQL
	 * @param startLimit 开始行数
	 * @param pageSize 每页显示件数
	 * @return SQL
	 */
	public String getLimitString(String sql, int startLimit, int pageSize)
	{
		sql = trim(sql);
		StringBuffer sb = new StringBuffer(sql.length() + 20);
		sb.append(this.trim(sql));
		sb.append(" limit ");
		sb.append(startLimit);
		sb.append(",");
		sb.append(pageSize);
		return sb.toString();
	}
	
	/**
	 * 是否支持Limit处理
	 * @return 
	 */
	public boolean supportsLimit()
	{
		return true;
	}

	/**
	 * SQL Trim
	 * @param sql SQL
	 * @return Trim后的SQL
	 */
	private String trim(String sql)
	{
		sql = sql.trim();
		if (sql.endsWith(SQL_END_DELIMITER))
		{
			sql = sql.substring(0, sql.length() - 1 - SQL_END_DELIMITER.length());
		}
		return sql;
	}
}
